local extension = Package:new("wq_wuyan")
extension.extensionName = "FkWqmt"
Fk:loadTranslationTable{
  ["wq_wuyan"] = "无期迷途·无厌",
}

local Wqc = require "packages/FkWqmt/wq_common"

-- 禁闭者：伊琳娜
local wq_yilinna = General:new(extension, "wq_yilinna", "qun", 3, 3, General.Female)
-- 技能：湮灭
local wq_skill_yanmie = fk.CreateActiveSkill{
  name = "wq_skill_yanmie",
  anim_type = "offensive",
  card_num = function (self)
    return 1 + Self:getMark("wq_skill_muhou_mark")
  end,
  target_num = 1,
  prompt = function (self, selected, selected_cards)
    local num1 = 1 + Self:getMark("wq_skill_muhou_mark")
    local num2 = 2 + Self:getMark("wq_skill_muhou_mark")
    return "#wq_skill_yanmie_prompt:::"..num1..":"..num2
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  card_filter = function(self, to_select, selected)
    if Self:getMark("wq_skill_muhou_mark") == 0 then
      return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Player.Hand and (not Self:prohibitDiscard(Fk:getCardById(to_select)))
    else
      return #selected < 2 and Fk:currentRoom():getCardArea(to_select) == Player.Hand and (not Self:prohibitDiscard(Fk:getCardById(to_select)))
    end
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    for _, cid in ipairs(effect.cards) do
      room:throwCard(cid, self.name, player, player)
    end
    if #target:getCardIds("e") > 0 then
      local num = math.min(1 + player:getMark("wq_skill_muhou_mark"), #target:getCardIds("e"))
      local ids = room:askForCardsChosen(player, target, 1, num, "e", self.name)
      room:throwCard(ids, self.name, target, player)
    elseif target:getMark("wq_skill_yanmie_mark-loseEquip") > 0 then
      local num = 2 + player:getMark("wq_skill_muhou_mark")
      room:damage{
        from = player,
        to = target,
        damage = num,
        skillName = self.name,
      }
    else
      local num = 1 + player:getMark("wq_skill_muhou_mark")
      room:damage{
        from = player,
        to = target,
        damage = num,
        skillName = self.name,
      }
    end
    if player:isAlive() then
      room:setPlayerMark(player, "wq_skill_muhou_mark", 0)
    end
  end,
}
local wq_skill_yanmie_buff = fk.CreateTriggerSkill{
  name = "#wq_skill_yanmie_buff",
  mute = true,
  refresh_events = {fk.EventPhaseEnd, fk.AfterCardsMove, fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill(self) and player.phase == Player.Play then
      if event == fk.EventPhaseEnd then
        return target == player
      elseif event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.from and move.from ~= player.id then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerEquip then
                return true
              end
            end
          end
        end
      elseif event == fk.EventLoseSkill then
        return target == player and data == self
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.from and move.from ~= player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip then
              room:setPlayerMark(room:getPlayerById(move.from), "wq_skill_yanmie_mark-loseEquip", 1)
            end
          end
        end
      end
    elseif event == fk.EventPhaseEnd or event == fk.EventLoseSkill then
      for _, p in ipairs(room:getOtherPlayers(player)) do
        room:setPlayerMark(p, "wq_skill_yanmie_mark-loseEquip", 0)
      end
    end
  end,
}
wq_skill_yanmie:addRelatedSkill(wq_skill_yanmie_buff)
-- 技能：幕后
local wq_skill_muhou = fk.CreateTriggerSkill{
  name = "wq_skill_muhou",
  anim_type = "control",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self) and data.card:isCommonTrick() and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_muhou_prompt")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "wq_skill_muhou_mark", 1)
  end,
  refresh_events = {fk.RoundEnd},
  can_refresh = function(self, event, target, player, data)
    return player:getMark("wq_skill_muhou_mark") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "wq_skill_muhou_mark", 0)
  end,
}
-- 技能：合谋
local wq_skill_hemou = fk.CreateTriggerSkill{
  name = "wq_skill_hemou",
  anim_type = "drawcard",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target:distanceTo(player) == 1
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_hemou_prompt::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:drawCards(player, 1, self.name)
    room:drawCards(target, 1, self.name)
  end,
}
-- 伊琳娜技能
wq_yilinna:addSkill(wq_skill_yanmie)
wq_yilinna:addSkill(wq_skill_muhou)
wq_yilinna:addSkill(wq_skill_hemou)
-- 伊琳娜的翻译
Fk:loadTranslationTable{
  ["wq_yilinna"] = "伊琳娜",
  ["#wq_yilinna"] = "局部异变",
  ["designer:wq_yilinna"] = "妙霄",
  ["cv:wq_yilinna"] = "官方",
  ["illustrator:wq_yilinna"] = "官方",
  ["wq_skill_yanmie"] = "湮灭",
  [":wq_skill_yanmie"] = "出牌阶段限一次，你可以弃置1张手牌，并选择一名其他角色。若其装备区中有牌，你弃置其装备区里至多1张牌；否则，若其于此阶段内失去过装备区里的牌，你对其造成2点伤害；否则，你对其造成1点伤害。",
  ["$wq_skill_yanmie"] = "湮灭吧！",
  ["#wq_skill_yanmie_prompt"] = "湮灭：你可以弃置%arg张手牌，并选择一名其他角色：<br>若其装备区中有牌，你弃置其装备区里至多%arg张牌<br>否则，若其于此阶段内失去过装备区里的牌，你对其造成%arg2点伤害<br>否则，你对其造成%arg点伤害",
  ["wq_skill_muhou"] = "幕后",
  [":wq_skill_muhou"] = "每轮限一次，其他角色使用普通锦囊牌时，你可以令你本轮下一次发动【湮灭】时描述中的所有数字+1。",
  ["$wq_skill_muhou"] = "一切都在计划之中。",
  ["#wq_skill_muhou_prompt"] = "幕后：你可以令你本轮下一次发动【湮灭】时描述中的所有数字+1",
  ["wq_skill_hemou"] = "合谋",
  [":wq_skill_hemou"] = "与你距离为1的角色的结束阶段，你可以与其分别摸一张牌。",
  ["$wq_skill_hemou"] = "时机已经成熟，我亲爱的……共犯。",
  ["#wq_skill_hemou_prompt"] = "合谋：你可以与%dest分别摸一张牌",
  ["~wq_yilinna"] = "我不能……倒在这里……",
}

-- 禁闭者：切尔西伯爵
local wq_qieerxibojue = General:new(extension, "wq_qieerxibojue", "qun", 4, 4, General.Female)
-- 技能：猫咪
local wq_skill_maomi = fk.CreateTriggerSkill{
  name = "wq_skill_maomi",
  anim_type = "special",
  events = {fk.EventPhaseStart, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Start and (not player:isNude()) and #player:getPile("wq_pile_mao") < 2
      elseif event == fk.DamageInflicted then
        return #player:getPile("wq_pile_mao") > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cids = nil
    if event == fk.DamageInflicted then
      cids = room:askForCard(player, 1, 1, false, self.name, true, ".|.|.|wq_pile_mao", "#wq_skill_maomi_prompt-discard", "wq_pile_mao")     
    else
      local maxNum = 2 - #player:getPile("wq_pile_mao")
      cids = room:askForCard(player, 1, maxNum, true, self.name, true, nil, "#wq_skill_maomi_prompt-choose:::"..maxNum)
    end
    if #cids > 0 then
      self.cost_data = cids
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(self.cost_data)
      player:addToPile("wq_pile_mao", dummy, true, self.name)
      room:drawCards(player, #self.cost_data, self.name)
    elseif event == fk.DamageInflicted then
      room:moveCardTo(self.cost_data, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name, "wq_pile_mao", true, player.id)
      data.damage = data.damage - 1
      if data.damage < 1 then
        return true
      end
    end
  end,
}
-- 技能：石化
local wq_skill_shihua = fk.CreateActiveSkill{
  name = "wq_skill_shihua",
  anim_type = "control",
  card_num = 1,
  target_num = 1,
  expand_pile = "wq_pile_mao",
  prompt = function (self, selected, selected_cards)
    return "#wq_skill_shihua_prompt"
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and #player:getPile("wq_pile_mao") > 0
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Self:getPileNameOfId(to_select) == "wq_pile_mao"
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and #Fk:currentRoom():getPlayerById(to_select).player_cards[Player.Judge] == 0
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cid = effect.cards[1]
    if Fk:getCardById(cid, true).sub_type == Card.SubtypeDelayedTrick then
      room:moveCardTo(Fk:getCardById(cid, true), Player.Judge, target, fk.ReasonJustMove, self.name)
    else
      local card = Fk:cloneCard("indulgence")
      if Fk:getCardById(cid, true).color == Card.Black then
        card = Fk:cloneCard("supply_shortage")
      end
      card:addSubcard(cid)
      target:addVirtualEquip(card)
      room:moveCardTo(card, Player.Judge, target, fk.ReasonJustMove, self.name)
    end
  end,
}
-- 技能：璀璨
local wq_skill_cuican = fk.CreateActiveSkill{
  name = "wq_skill_cuican",
  anim_type = "special",
  card_num = 0,
  target_num = 1,
  prompt = "#wq_skill_cuican_prompt",
  frequency = Skill.Limited,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and #player:getPile("wq_pile_mao") < 2
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and #Fk:currentRoom():getPlayerById(to_select):getCardIds("ej") > 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cid = room:askForCardChosen(player, target, "ej", self.name)
    if cid then
      player:addToPile("wq_pile_mao", cid, true, self.name)
      if player:isAlive() then
        room:recover {
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name
        }
      end
      player:setSkillUseHistory("wq_skill_shihua", 0, Player.HistoryPhase)
    end
  end,
}
-- 切尔西伯爵的技能
wq_qieerxibojue:addSkill(wq_skill_maomi)
wq_qieerxibojue:addSkill(wq_skill_shihua)
wq_qieerxibojue:addSkill(wq_skill_cuican)
-- 切尔西伯爵的翻译
Fk:loadTranslationTable{
  ["wq_qieerxibojue"] = "切尔西伯爵",
  ["#wq_qieerxibojue"] = "晶体矿质",
  ["designer:wq_qieerxibojue"] = "妙霄",
  ["cv:wq_qieerxibojue"] = "官方",
  ["illustrator:wq_qieerxibojue"] = "官方",
  ["wq_skill_maomi"] = "猫咪",
  [":wq_skill_maomi"] = "准备阶段，你可以将至多X张牌置于你的武将牌上，称为「猫」，然后摸等量的牌（X为2-你武将牌上已有的「猫」的数量）。当你受到伤害时，你可以移去一张「猫」，令此伤害-1。",
  ["$wq_skill_maomi"] = "走吧，西迪。",
  ["wq_pile_mao"] = "猫",
  ["#wq_skill_maomi_prompt-choose"] = "猫咪：你可以将至多 %arg 张牌置于你的武将牌上称为「猫」，然后摸等量的牌",
  ["#wq_skill_maomi_prompt-discard"] = "猫咪：你可以移去一张「猫」令此伤害-1",
  ["wq_skill_shihua"] = "石化",
  [":wq_skill_shihua"] = "出牌阶段限一次，你可以将一张「猫」置入一名判定区中没有牌的其他角色的判定区中。若此牌不是延时锦囊牌，则红色牌视为【乐不思蜀】，黑色牌视为【兵粮寸断】。",
  ["$wq_skill_shihua"] = "西迪，拦住他们!",
  ["#wq_skill_shihua_prompt"] = "石化：你可以将一张「猫」置于一名判定区中没有牌的其他角色的判定区中<br>若此牌不是延时锦囊牌，则红色牌视为【乐不思蜀】，黑色牌视为【兵粮寸断】",
  ["wq_skill_cuican"] = "璀璨",
  [":wq_skill_cuican"] = "限定技，出牌阶段，若你武将牌上的「猫」不足两张，你可以将场上的一张牌置于武将牌上，称为「猫」。若如此做，你回复一点体力，然后你令技能【石化】视为此阶段内未发动过。",
  ["$wq_skill_cuican1"] = "我认为我就是为宝石而生的……但我真的和它们一样耀眼吗？",
  ["$wq_skill_cuican2"] = "以后，没有人能伤害我们，没有人。",
  ["#wq_skill_cuican_prompt"] = "璀璨：你可以将场上的一张牌置于武将牌上称为「猫」，回复一点体力，然后令技能【石化】视为此阶段内未发动过",
  ["~wq_qieerxibojue"] = "怎么会……这样？",
}

return extension